package com.qf.fmall.indeximg.controller;

import com.qf.fmall.category.entity.Category;
import com.qf.fmall.category.service.ICategoryService;
import com.qf.fmall.indeximg.entity.IndexImg;
import com.qf.fmall.indeximg.service.IIndexImgService;
import com.qf.fmall.product.entity.Product;
import com.qf.fmall.product.service.IProductService;
import com.qf.fmall.utils.ResultVo;
import com.qf.fmall.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 轮播图 前端控制器
 * </p>
 *
 * @author wbw
 * @since 2023-11-28
 */
@RestController
@CrossOrigin
@RequestMapping("/index")
@Slf4j
public class IndexImgController {
    @Autowired
    private IIndexImgService indexImgService;
    @GetMapping ("/indeximg")
    public ResultVo carousel(HttpServletRequest request){
        //1.打印日志，把该接口接收到的参数打印出来
        log.info("查询轮播图数据接口");
        String token = request.getHeader("token");
        Boolean flag = TokenUtils.checkToken(token);
        if (flag.equals(false)){
            //token无效
            log.info("token无效，token={}",token);
            return ResultVo.notok(10005,"token error");
        }

        //下面的方式适用于方法简单的时候
//        //2.进行需要的参数校验
//        //3.调用service层的对象进行业务处理
//        QueryWrapper<IndexImg> wrapper = new QueryWrapper<>();
//        wrapper.eq("status",1);//where status = 1
//        //排序 order by seq
//        wrapper.orderByAsc("seq");
//        List<IndexImg> data = indexImgService.list(wrapper);
        List<IndexImg> data = indexImgService.selectImg();
        //4.返回Json
        return ResultVo.ok(10000,"success",data);
  }

    @Autowired
    private ICategoryService categoryService;
    @GetMapping("/category-list")
    public ResultVo category(){
        //1.打印日志，把该接口接收到的参数打印出来
        log.info("获取分类信息接口");
        //3.调用service层的对象进行业务处理
//        List<Category> data = categoryService.categoryList(); //此方法查询数据库次数过多
        //下面为优化方法
        List<Category> data = categoryService.categoryListBetter();
        //4.返回Json
        return  ResultVo.ok(10000,"success",data);
    }
    @Autowired
    private IProductService productService;
    @GetMapping("/list-recommends")
    public ResultVo listRecommends(){
        //1.打印日志，把收到的参数打印出来
        log.info("查询商品信息");
        //2.进行必要的参数校验
        //3.调用serivce层的对象进行业务处理
        //4.返回json
        List<Product> data = productService.listRecommends();
        return ResultVo.ok(10000,"success",data);
    }
    @GetMapping("/category-recommends")
    public ResultVo categoryRecommends(){
        log.info("查询分类推荐商品信息");
        List<Category> data = categoryService.categoryRecommends();
        return ResultVo.ok(10000,"seccess",data);

    }
}
